package com.dreamstations.changshads.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

import com.dreamstations.changshads.R;
import com.dreamstations.changshads.adapter.Home_FragmentAdapter;
import com.dreamstations.changshads.fragment.Figure_Leading_Frag;
import com.dreamstations.changshads.fragment.Figure_Simple_Frag;
import com.dreamstations.changshads.fragment.Library_Header_Frag;
import com.dreamstations.changshads.fragment.Library_Simple_Frag;
import com.dreamstations.changshads.fragment.Resource_Header_Frag;
import com.dreamstations.changshads.fragment.Resource_Simple_Frag;
import com.dreamstations.changshads.fragment.Story_Header_Frag;
import com.dreamstations.changshads.fragment.Story_Simple_Frag;
import com.dreamstations.changshads.util.RequestType;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 7YHong on 2016/6/19.
 */
public class SectionActivity extends AppCompatActivity {
    public static final String TAG = SectionActivity.class.getName();
    public static final int SECTION_FIGURE = 0;
    public static final int SECTION_STORY = 1;
    public static final int SECTION_RESOURCE = 2;
    public static final int SECTION_LIBRARY = 3;

    Home_FragmentAdapter fragmentAdapter;
    TabLayout tablayout;
    ViewPager viewPager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.section_activity);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();;
            }
        });

        tablayout = (TabLayout) findViewById(R.id.toolbar_content_tablayout);
        viewPager = (ViewPager) findViewById(R.id.section_viewpager);
        int sectionType = getIntent().getIntExtra("sectionType", -1);
        initFragments(sectionType);
    }

    void initFragments(int sectionType) {
        switch (sectionType) {
            case SECTION_FIGURE: {
                loadFigure();
            }
            break;
            case SECTION_STORY: {
                loadStory();
            }
            break;
            case SECTION_RESOURCE: {
                loadResource();
            }
            break;
            case SECTION_LIBRARY: {
                loadLibrary();
            }
            break;
            default: {
                Toast.makeText(getApplicationContext(), "找不到合适的Fragment", Toast.LENGTH_SHORT).show();
            }
            break;
        }
    }

    private void loadFigure() {
        List<Fragment> fragments = new ArrayList<>();
        fragmentAdapter = new Home_FragmentAdapter(getSupportFragmentManager(), fragments);
        fragments.add(new Figure_Simple_Frag("党和国家领导人", RequestType.TYPE_FIGURE,1));
        fragments.add(new Figure_Simple_Frag("高级将帅", RequestType.TYPE_FIGURE,2));
        fragments.add(new Figure_Simple_Frag("两院院士", RequestType.TYPE_FIGURE,3));
        fragments.add(new Figure_Simple_Frag("风云人物", RequestType.TYPE_FIGURE,4));
        fragments.add(new Figure_Simple_Frag("历任市委书记", RequestType.TYPE_FIGURE,5));
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setOffscreenPageLimit(5);
        tablayout.setupWithViewPager(viewPager);
    }

    private void loadStory() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new Story_Simple_Frag("历次党代会", RequestType.TYPE_STORY,1));
        fragments.add(new Story_Simple_Frag("重大事件", RequestType.TYPE_STORY,2));
        fragments.add(new Story_Simple_Frag("口述长沙", RequestType.TYPE_STORY,3));
        fragments.add(new Story_Simple_Frag("往事珍闻", RequestType.TYPE_STORY,4));
        fragments.add(new Story_Simple_Frag("历史留影", RequestType.TYPE_STORY,5));
        fragmentAdapter = new Home_FragmentAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setOffscreenPageLimit(5);
        tablayout.setupWithViewPager(viewPager);

    }

    private void loadResource() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new Resource_Simple_Frag("党史馆藏", RequestType.TYPE_RESOURCE,1));
        fragments.add(new Resource_Simple_Frag("党史场馆", RequestType.TYPE_RESOURCE,2));
        fragments.add(new Resource_Simple_Frag("红色遗迹", RequestType.TYPE_RESOURCE,3));
        fragmentAdapter = new Home_FragmentAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setOffscreenPageLimit(3);
        tablayout.setupWithViewPager(viewPager);

    }

    private void loadLibrary() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new Library_Simple_Frag("历年成果", RequestType.TYPE_LIBRARY,1));
        fragments.add(new Library_Simple_Frag("党史书屋", RequestType.TYPE_LIBRARY,2));
        fragments.add(new Library_Simple_Frag("在线阅读", RequestType.TYPE_LIBRARY,3));
        fragmentAdapter = new Home_FragmentAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setOffscreenPageLimit(3);
        tablayout.setupWithViewPager(viewPager);

    }
}
